// import { makeInvoiceApi, modifyInvoiceApi, saveDraftApi } from "@/api/client/documentManagement/waybillManagement";
import { saveWarehouseOrderpi, updateWarehouseOrderApi } from "@/api/waybillManagement/warehouseShipment/index";
import FormHook from "@/hook/formHook";
import { GeneraDataType } from "@/interface";
import { message } from "ant-design-vue";
import { computed } from "vue";
export default function OperationHook(state: GeneraDataType) {
  const { formState, formDataRef, emit, loading, dialogOpts, waybillInfo, drawerOpts, type, tableRef, passData } = state;
  const { resetFields, validateZhInputHandle } = FormHook({ formState });
  const selectRows = computed<Array<ClientWaybillManagement.ListRows>>(() => {
    // return tableRef.value.getCheckboxRecords() || [];
    return formState.preOrderShipmentBox
      .filter((t: any) => t.choose && t.fbaBoxNumber)
      .map((q: any) => {
        return { ...q, choose: undefined };
      });
  });

  const submitHandle = () => {
    console.log(selectRows.value);
    if ((!formState.materialQualityId || !formState.materialQualityId.length) && type.value === 1) return message.error("请勾选材质!");
    if (!selectRows.value.length && type.value === 1) return message.error("请勾选产品信息!");

    formDataRef.value.validate().then(() => {
      const params = {
        ...formState,
        skuIdList: type.value === 1 ? selectRows.value.map((t: any) => t.skuId).filter((t: any) => t) : undefined,
        materialQualityId: type.value === 1 ? formState.materialQualityId : undefined,
        waybillId: passData.value.id,
        preOrderShipmentBox: selectRows.value
      };
      params.materialQualityId = formState.materialQualityId.join(",");
      params.preOrderShipmentBox = params.preOrderShipmentBox.filter((t: ClientWaybillManagement.WaybillBox) => t.fbaBoxNumber);
      loading.value = true;

      let requestFn;
      if (type.value === 2) {
        requestFn = updateWarehouseOrderApi;
      } else {
        requestFn = saveWarehouseOrderpi;
      }

      return requestFn(params)
        .then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("操作成功");
            emit("submit");
          }
        })
        .finally(() => (loading.value = false));
    });
  };
  // const saveDraftHandle = () => {
  //   formDataRef.value.validate().then(() => {
  //     const params = {
  //       ...formState
  //     };
  //     params.materialQualityId = formState.materialQualityId.join(",");
  //     loading.value = true;
  //     let requestFn;
  //     if (type.value === 1) requestFn = saveDraftApi;
  //     else requestFn = modifyInvoiceApi;
  //     return requestFn(params).then((res: any) => {
  //       const { code } = res;
  //       if (code === 200) {
  //         message.success("制作发票成功!");
  //         emit("submit");
  //       }
  //     });
  //   });
  // };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  const dialogSubmitHandle = (state: any, uploadType: number) => {
    dialogOpts.visible = false;
    if (uploadType === 1) {
      formState.preOrderShipmentBox = state.preOrderShipmentBox || [];
      const declaredValueList = state.preOrderShipmentBox.map((t: ClientWaybillManagement.WaybillBox) => t.productDeclarePriceAll || 0);
      formState.declaredValue = declaredValueList.reduce((a: number, b: number) => {
        return Number(a) + Number(b);
      });
    } else {
      Object.assign(formState, state);
      formState.addressType = String(state.addressType);
      formState.materialQualityId = state.materialQualityId?.split(",");
      const declaredValueList = state.preOrderShipmentBox.map((t: ClientWaybillManagement.WaybillBox) => t.productDeclarePriceAll || 0);
      formState.declaredValue = declaredValueList.reduce((a: number, b: number) => {
        return Number(a) + Number(b);
      });
    }
  };
  const drawerSubmitHandle = (e: any) => {
    drawerOpts.visible = false;
    if (drawerOpts.component === "ChooseWarehouse") {
      const { zipCode, warehouseCode, receiverName, state, company, address, city } = e;
      formState.warehouseCode = warehouseCode;
      formState.zipCode = zipCode;
      formState.receiverName = receiverName;
      formState.state = state;
      formState.receiverCompany = company;
      formState.address = address;
      formState.city = city;
    } else if (drawerOpts.component === "ChooseAddress") {
      const { postcode, contact, state, companyName, address, city, phone, id } = e;
      formState.zipCode = postcode;
      formState.receiverName = contact;
      formState.state = state;
      formState.receiverCompany = companyName;
      formState.address = address;
      formState.city = city;
      formState.receiverPhone = phone;
      formState.addressId = id;
    }
  };
  const importInvoiceHandle = () => {
    dialogOpts.title = "发票上传";
    dialogOpts.width = "520px";
    dialogOpts.type = type.value;
    dialogOpts.passData = waybillInfo.value;
    dialogOpts.component = "ImportInvoice";
    dialogOpts.visible = true;
  };
  // 选择仓库
  const chooseWarehouseHandle = () => {
    drawerOpts.title = "选择仓库";
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.passData = formState;
    drawerOpts.component = "ChooseWarehouse";
    drawerOpts.visible = true;
  };
  // 选择 非FBA地址
  const chooseAddressHandle = () => {
    drawerOpts.title = "选择私人地址";
    drawerOpts.width = "1200px";
    drawerOpts.type = 1;
    drawerOpts.passData = formState;
    drawerOpts.component = "ChooseAddress";
    drawerOpts.visible = true;
  };
  const changeAddressTypeHandle = () => {
    formState.warehouseCode = undefined;
    formState.zipCode = undefined;
    formState.receiverName = undefined;
    formState.state = undefined;
    formState.receiverCompany = undefined;
    formState.address = undefined;
    formState.city = undefined;
    formState.receiverPhone = undefined;
    formState.addressId = undefined;
  };
  const selectChangeEvent = (e: any) => {
    const { checked, row } = e || {};
    const shipmentNumber = row.shipmentBoxNumber || "";
    formState.preOrderShipmentBox.map((t: any) => {
      if (shipmentNumber === t.shipmentBoxNumber) {
        tableRef.value.setCheckboxRow(t, checked);
      }
    });
  };
  return {
    submitHandle,
    // saveDraftHandle,
    resetHandle,
    resetFields,
    validateZhInputHandle,
    dialogSubmitHandle,
    importInvoiceHandle,
    drawerSubmitHandle,
    chooseWarehouseHandle,
    chooseAddressHandle,
    changeAddressTypeHandle,
    selectChangeEvent
  };
}
